/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// May 1, 2009

package com.sceneworker.app.globals;

import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;

import com.jme.math.Vector3f;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.system.DisplaySystem;
import com.jme2utils.staticutils.CameraUtils;
import com.sceneworker.app.globals.cacheobjects.ISceneWorkerCacheObject;

public class SceneWorkerGlobals {
	static 	final public	Font 	SMALL_FONT = new Font("Helvetica", Font.PLAIN, 10);
	static 	final public	Font 	MEDIUM_FONT = new Font("Helvetica", Font.PLAIN, 12);
	static 	final public	Font 	LARGE_FONT = new Font("Helvetica", Font.PLAIN, 14);
	
	static 	final public	Dimension 	PREFERRED_NUMBER_TEXTFIELD_DIM = new Dimension(50, 17);
	static 	final public	Dimension 	MAX_NUMBER_TEXTFIELD_DIM = new Dimension(100, 17);
	static 	final public	Dimension 	PREFERRED_TEXTFIELD_DIM = new Dimension(150, 17);
	static 	final public	Dimension 	MAX_TEXTFIELD_DIM = new Dimension(350, 17);
	
	static	private		SceneWorkerGlobals		mcl_inst;

	static	public	SceneWorkerGlobals	inst() {
		if(mcl_inst == null) {
			mcl_inst = new SceneWorkerGlobals();
		}

		return mcl_inst;
	}

	private		boolean		mb_integerSnapOn;
	private		boolean		mb_zoomOnSelectedObjects;
	private		String		mstr_curDir;
	private		boolean		mb_dropAtCamera;
	private		boolean		mb_renderLights = true;
	
	// cache is just a temp array that we may want to store data that otherwise might be lost after an object is created...e.g. terrains and the height map used to create it
	private		Hashtable<String, ISceneWorkerCacheObject>		mcla_cache;
	static	public		String		DATA_DIR = "com/sceneworker/data/";
	static	public		String		SCENEWORKER_SHADER_DIR = "com/sceneworker/data/shaders/";
	static	public		String		SCENEWORKER_SCRIPTS_DIR = "com/sceneworker/data/scripts/";

	public	SceneWorkerGlobals() {
		mb_zoomOnSelectedObjects = false;
		mstr_curDir = System.getProperty("user.dir");
		mcla_cache = new Hashtable<String, ISceneWorkerCacheObject>();
	}
	
	public	void	addToCache(ISceneWorkerCacheObject cl_obj) {
		mcla_cache.put(cl_obj.getUniqueID(), cl_obj);
	}
	
	public	ISceneWorkerCacheObject		getFromCache(String str_key) {
		return mcla_cache.get(str_key);
	}
	
	public	ArrayList<Object>		getAllInstancesInCache(Class<?> cl_class) {
		ArrayList<Object>		lcla_inst = new ArrayList<Object>();
		
		Collection<ISceneWorkerCacheObject>		lcla_values = mcla_cache.values();
		
		for(ISceneWorkerCacheObject lcl_cache : lcla_values) {
			if(lcl_cache.getClass() == cl_class) {
				lcla_inst.add(lcl_cache);
			}
		}
		
		return lcla_inst;
	}
	
	public	void	removeFromCache(String str_key) {
		mcla_cache.remove(str_key);
	}
	
	public	boolean		getRenderLights() {
		return mb_renderLights;
	}
	
	public	void	setRenderLights(boolean b_renderLights) {
		mb_renderLights = b_renderLights;
	}
	
	public	boolean		getIntegerSnapOn() {
		return mb_integerSnapOn;
	}
	
	public	void		setIntegerSnapOn(boolean b_snapOn) {
		mb_integerSnapOn = b_snapOn;
	}
	
	public	boolean		getZoomOnSelectedObjects() {
		return mb_zoomOnSelectedObjects;
	}
	
	public	void	setZoomOnSelectedObjects(boolean b_zoom) {
		mb_zoomOnSelectedObjects = b_zoom;
	}

	public	String	getCurrentDirectory() {
		return mstr_curDir;
	}
	
	public	void	setCurrentDirectory(String str_curDir) {
		mstr_curDir = str_curDir;
	}
	
	public	boolean	getDropAtCamera() {
		return mb_dropAtCamera;
	}
	
	public	void	setDropAtCamera(boolean b_drop) {
		mb_dropAtCamera = b_drop;
	}
	
	public	void	pasteSpatial(Node cl_par, Spatial cl_sp) {
		cl_par.attachChild(cl_sp);
		
		if(mb_dropAtCamera) {
			Vector3f		lcl_camPos = DisplaySystem.getDisplaySystem().getRenderer().getCamera().getLocation().clone();
			Vector3f		lcl_localCamPos = new Vector3f();
			cl_par.worldToLocal(lcl_camPos, lcl_localCamPos);
			cl_sp.setLocalTranslation(lcl_localCamPos);
			CameraUtils.moveCameraInFrontOfObject(cl_sp);
			
		}
	}
}
